package org.dicker.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dicker.biz.UserBiz;
import org.dicker.biz.impl.UserBizImpl;
import org.dicker.entity.UserInfo;
import org.lanqiao.mvc.entity.ModelAndView;
import org.lanqiao.mvc.entity.YockMvcAnnotation.Controller;
import org.lanqiao.mvc.entity.YockMvcAnnotation.RequestMapping;
import org.lanqiao.mvc.entity.YockMvcAnnotation.ResponseDispatch;
import org.lanqiao.mvc.entity.YockMvcAnnotation.ResponseVoid;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

@Controller
public class IndexController {

	private UserBiz uBiz=new UserBizImpl();
	
	@RequestMapping(value = "/index.do") 
	@ResponseDispatch(value = "/WEB-INF/console/index.jsp")
	public ModelAndView index(HttpServletRequest req,HttpServletResponse res){
		ModelAndView mv = new ModelAndView();
		return mv;
	}
	@RequestMapping(value = "/welcome.do")
	@ResponseDispatch(value = "/WEB-INF/console/welcome.jsp")
	public ModelAndView wel(HttpServletRequest req,HttpServletResponse res){
		ModelAndView mv = new ModelAndView();
		
		return mv;
	}
	
	@RequestMapping(value = "/error.do") 
	@ResponseDispatch(value = "/WEB-INF/console/error.jsp")
	public ModelAndView err(HttpServletRequest req,HttpServletResponse res){
		ModelAndView mv = new ModelAndView();
		return mv;
	}
	
	@RequestMapping("/wx.do")
	@ResponseVoid
	public void wx(
			HttpServletRequest request,
			HttpServletResponse response
			)
			throws Exception{
		
		//userJson
		String userJson = (String) request.getAttribute("userJson");
		JSONObject jsonObj  = JSONUtil.parseObj(userJson);
	    String openid = jsonObj.getStr("openid");
	    String nickname = jsonObj.getStr("nickname");
	    String headimgurl = jsonObj.getStr("headimgurl");
	    
	    
	    UserInfo uInfo=new UserInfo();
	    uInfo.setWx_openid(openid);
	    uInfo.setUser_name(nickname);
	    uInfo.setUser_img(headimgurl);
	    //////////////////////此处省略存入数据库的步骤//////////////////////
	    if(uBiz.selectUserid(openid)==null){
	    	uBiz.addUser(uInfo);
	    }
	    
	    response.setContentType("text/html; charset=UTF-8");
		String 前端地址="/randomNotes/index.html?wx_openid="+openid;
		response.getWriter().print("<script>location.href='"+前端地址+"';</script>");
	}
}

